package com.wedistudio.slideshare.xml;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;

import com.wedistudio.android.framework.fetcher.URLBuilder;
import com.wedistudio.android.framework.json.JSonObject;
import com.wedistudio.slideshare.SlideshareUtils;
import com.wedistudio.slidesprovider.Author;
import com.wedistudio.slidesprovider.Slide;
import com.wedistudio.slidesprovider.SlideID;
import com.wedistudio.slidesprovider.SlideTag;

@Root
public class Slideshow extends JSonObject<Slideshow> {
	@Element(required = false)
	public String ID;
	@Element(required = false)
	public String Title;
	@Element(required = false)
	public String Description;
	// 0 if queued for conversion, 1 if converting, 2 if converted, 3 if
	// conversion failed
	@Element(required = false)
	public Integer Status;
	@Element(required = false)
	public String Username;
	@Element(required = false)
	public String URL;
	@Element(required = false)
	public String ThumbnailURL;
	@Element(required = false)
	public String ThumbnailSmallURL;
	@Element(required = false)
	public String Embed;
	@Element(required = false)
	public String Created;
	@Element(required = false)
	public String Updated;
	@Element(required = false)
	public String Language;
	// ppt (or pdf, pps, odp, doc, pot, txt, rdf)
	@Element(required = false)
	public String Format;
	@Element(required = false)
	public Integer Download;
	@Element(required = false)
	public String DownloadUrl;
	// 0 if presentation, 1 if document, 2 if a portfolio, 3 if video
	@Element(required = false)
	public Integer SlideshowType;
	@Element(required = false)
	public Integer InContest;
	@Element(required = false)
	public String UserID;
	@Element(required = false)
	public String ExternalAppUserID;
	@Element(required = false)
	public String ExternalAppID;
	@Element(required = false)
	public String PPTLocation;
	@Element(required = false)
	public String StrippedTitle;
	@ElementList(required = false)
	public List<Tag> Tags;
	@Element(required = false)
	public Integer Audio;
	@Element(required = false)
	public Integer NumDownloads;
	@Element(required = false)
	public Integer NumViews;
	@Element(required = false)
	public Integer NumComments;
	@Element(required = false)
	public Integer NumFavorites;
	@Element(required = false)
	public Integer NumSlides;
	@ElementList(required = false)
	public List<RelatedSlideshowID> RelatedSlideshows;
	@Element(required = false)
	public Integer PrivacyLevel;
	@Element(required = false)
	public Integer FlagVisible;
	@Element(required = false)
	public Integer ShowOnSS;
	@Element(required = false)
	public Integer SecretURL;
	@Element(required = false)
	public Integer AllowEmbed;
	@Element(required = false)
	public Integer ShareWithContacts;
	@Element(required = false)
	public String ThumbnailSize;
	@Element(required = false)
	public String Transcript;

	public Slide getSlide() {
		try {
			Slide slide = Slide.create(SlideID.create(ID,
					URLBuilder.create(URL)));
			slide.setAuthor(Author.create(SlideshareUtils.SOURCE, Username));
			slide.setSummary(Description);
			slide.setTitle(Title);
			slide.setThumbnail(URLBuilder.create("http:" + ThumbnailURL));
			SimpleDateFormat sdf = new SimpleDateFormat(
					"EEE MMM d HH:mm:ss Z yyyy", Locale.ENGLISH);
			slide.setPublicationDate(Updated == null ? sdf.parse(Created) : sdf
					.parse(Updated));
			slide.setNbSlides(NumSlides);
			if (RelatedSlideshows != null) {
				for (RelatedSlideshowID id : RelatedSlideshows)
					slide.addRelated(SlideshareUtils.SOURCE, id.slideshareId);
			}

			if (Tags != null) {
				for (Tag tag : Tags) {
					slide.addTag(SlideTag.create(SlideshareUtils.SOURCE, tag.tag));
				}
			}
			return slide;
		} catch (Throwable e) {
			throw new RuntimeException(e);
		}
	}

}